//
//  ViewController.m
//  20502
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btnPressed;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *ActivityView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)BtnPressed:(id)sender
{
    NSDate *date = [NSDate date];
    [self.ActivityView startAnimating];
    
    NSString *str1 = [self doSomething1];
    NSString *str2 = [self doSomething2:str1];
    NSString *str3 = [self doSomething3:str1];
    NSString *str4 = [self doSomething4:str1];
    
    self.textView.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
    [self.ActivityView stopAnimating];
    NSDate *d2 = [NSDate date];
    NSTimeInterval interval = [d2 timeIntervalSinceDate:date];
    NSLog(@"运行了%.3f秒",interval);
    
    
    
}


- (IBAction)Btn2Pressed:(id)sender
{
    
    // 开始的时间
    NSDate *d1 = [NSDate date];
    // 开始转动
    [self.ActivityView startAnimating];
    
    
    __weak ViewController *weakSelf = self;

    [self.ActivityView startAnimating];
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);// 并发

    dispatch_async(queue, ^{
        NSLog(@"当前在%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
        NSString *str1 = [self doSomething1];// 1
        
    
    dispatch_group_t group = dispatch_group_create();
    
    __block NSString *str2;
    __block NSString *str3;
    __block NSString *str4;
    
    dispatch_group_async(group, queue, ^{
        str2 = [weakSelf doSomething2:str1];
    });
    dispatch_group_async(group, queue, ^{
        str3 = [weakSelf doSomething3:str1];
    });
    dispatch_group_async(group, queue, ^{
        str4 = [weakSelf doSomething4:str1];
    });
    
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
 
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"当前在%@",[NSThread currentThread].isMainThread?@"主线程中":@"子线程中");
   
        self.textView.text = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
    
        [self.ActivityView stopAnimating];
        
       
        NSDate *d2 = [NSDate date];
      
        NSTimeInterval interval = [d2 timeIntervalSinceDate:d1];
        NSLog(@"运行了%.3f秒",interval);
    });
    
});

}





 - (IBAction)btn5Pressed:(id)sender
{
  
    [self.ActivityView startAnimating];
    
    [self performSelectorInBackground:@selector(calculate) withObject:nil];
}

- (void)calculate
{
    NSString *str1 = [self doSomething1];
    NSString *str2 = [self doSomething2:str1];
    NSString *str3 = [self doSomething3:str1];
    NSString *str4 = [self doSomething4:str1];
    NSString *result = [NSString stringWithFormat:@"%@\n%@\n%@\n%@",str1,str2,str3,str4];
    
       [self.textView performSelectorOnMainThread:@selector(setText:) withObject:result waitUntilDone:NO];

    [self.ActivityView performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
    
}
- (NSString *)doSomething1
{
    [NSThread sleepForTimeInterval:1];
    return @"从网络上获取一段信息:abc123";
}

- (NSString *)doSomething2:(NSString *)str
{
    [NSThread sleepForTimeInterval:2];
    return [str uppercaseString];
}

- (NSString *)doSomething3:(NSString *)str
{
    [NSThread sleepForTimeInterval:3];
    return [NSString stringWithFormat:@"%lu",str.length];
}

- (NSString *)doSomething4:(NSString *)str
{
    [NSThread sleepForTimeInterval:4];
    return [str stringByReplacingOccurrencesOfString:@"a" withString:@"A"];
}
@end
